package io.karl.code_gen.util;

import lombok.SneakyThrows;

import java.io.File;

public class FileUtil {

    @SneakyThrows
    public static void cleanDirectory(File directory) {
        if (!directory.exists()) {
            return;
        }
        if (!directory.isDirectory()) {
            throw new IllegalArgumentException("The provided path is not a directory.");
        }

        File[] files = directory.listFiles();
        if (files == null) {
            return;
        }

        for (File file : files) {
            if (file.isDirectory()) {
                cleanDirectory(file); // 递归删除子目录内容
            }
            if (!file.delete()) {
                System.err.println("Failed to delete file: " + file.getAbsolutePath());
            }
        }
    }
}
